home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 19 / develop 19 code / Adding GX Printing to QD Apps / Simple Sample ƒ / events.c next >
Encoding:
C/C++ Source or Header  |  1994-04-15  |  8.6 KB  |  375 lines  |  [TEXT/KAHL]

  1. /*********************************************************************
  2.  
  3.     events.c
  4.     
  5.     This file contains the event handling code for the QuickDraw GX
  6.     unaware sample, "Simple Sample."
  7.     
  8.     Additional info can be found in the related develop #19 article,
  9.     "Adding QuickDraw GX Printing to QuickDraw Applications."
  10.  
  11.     Dave Hersey, Apple Developer Technical Support.
  12.     
  13.     ——————— Edit Trail ———————
  14.     
  15.     begat:                                            1/22/94  - dmh
  16.     cleaned up for 2nd draft of develop article:    3/10/94  - dmh
  17.     cleaned up for final:                            4/14/94  - dmh
  18.     
  19. *********************************************************************/
  20.  
  21. #include "Simple Sample.h"
  22.  
  23.  
  24. /************************************************************
  25.   MyEventLoop - This routine (in conjunction with main)
  26.   makes up our event loop.
  27.  
  28.  *************************************************************/
  29.  
  30. void MyEventLoop()
  31. {
  32.     EventRecord        theEvent;
  33.  
  34.     if (WaitNextEvent(everyEvent, &theEvent, gSleep, nil))
  35.         MyDoEvent(&theEvent);
  36. }
  37.  
  38.  
  39. /************************************************************
  40.   MyDoEvent - This routine handles event dispatching.
  41.  
  42.  *************************************************************/
  43.  
  44. void MyDoEvent(EventRecord *theEvent)
  45. {
  46.     char            key;
  47.     WindowPtr         whichWindow;
  48.     MyDocumentPtr    windowDoc;
  49.  
  50.     switch(theEvent->what)
  51.     {                    
  52.         case updateEvt:            // Updating a window.
  53.  
  54.             if (((WindowPeek) theEvent->message)->windowKind == userKind)
  55.                 MyUpdateWindow((WindowPtr) theEvent->message);
  56.  
  57.             break;
  58.         
  59.         case mouseDown:            // Mousedown in a window or menu.
  60.     
  61.             switch (FindWindow(theEvent->where, &whichWindow))
  62.             {
  63.                 case inSysWindow:
  64.  
  65.                     SystemClick(theEvent, whichWindow);
  66.                     break;
  67.                         
  68.                 case inDrag:
  69.  
  70.                     if (((WindowPeek) whichWindow)->windowKind == userKind)
  71.                     {
  72.                         DragWindow(whichWindow, theEvent->where, &qd.screenBits.bounds);
  73.                         MyAdjustMenus();
  74.                     }
  75.                     break;
  76.  
  77.                 case inGoAway:
  78.  
  79.                     if ((((WindowPeek) whichWindow)->windowKind == userKind) &&
  80.                         (TrackGoAway(whichWindow, theEvent->where)))
  81.                     {
  82.                         MyDisposeDocument(MyGetDocPtr(whichWindow));
  83.                         MyAdjustMenus();
  84.                     }
  85.                     
  86.                     break;
  87.                 
  88.                 case inContent:
  89.  
  90.                     if (((WindowPeek) whichWindow)->windowKind == userKind)
  91.                     {
  92.                         SelectWindow(whichWindow);
  93.                         MyAdjustMenus();
  94.                     }
  95.                     
  96.                     break;
  97.  
  98.                 case inMenuBar:
  99.                     MyDoMenuCommand(MenuSelect(theEvent->where));
  100.                     MyAdjustMenus();
  101.                     break;
  102.             }
  103.  
  104.         case keyDown:            // Key pressed.
  105.         case autoKey:
  106.  
  107.             key = theEvent->message & charCodeMask;
  108.             if ((theEvent->modifiers & cmdKey) &&
  109.                 (theEvent->what == keyDown))
  110.                 {
  111.                     MyDoMenuCommand(MenuKey(key));
  112.                     MyAdjustMenus();
  113.                 }
  114.             
  115.             break;
  116.  
  117.         case kOSEvent:            // Operating system event.
  118.  
  119.             switch ((unsigned long) theEvent->message >> 24)
  120.             {
  121.                  case kSuspendResumeMessage:                            // Suspend event:
  122.                     if ((theEvent->message & kResumeMask) == 0)
  123.                         gSleep = 80;
  124.                     else                                            // Resume event:
  125.                     {
  126.                         gSleep = 0;
  127.                         SetCursor(&qd.arrow);
  128.                     }
  129.  
  130.  
  131.                 break;
  132.             }
  133.  
  134.             break;
  135.                 
  136.         case kHighLevelEvent:    // AppleEvent.
  137.  
  138.             AEProcessAppleEvent(theEvent);
  139.             break;
  140.     }
  141. }
  142.  
  143.  
  144. /************************************************************
  145.   MyDoAEInstallation - This routine installs our core
  146.   AppleEvent handlers.
  147.  
  148. *************************************************************/
  149.  
  150. void MyDoAEInstallation()
  151. {
  152.     AEInstallEventHandler(kCoreEventClass,
  153.                           kAEOpenApplication,
  154.                           (EventHandlerProcPtr) MyHandleOAPP,
  155.                           0, false);
  156.  
  157.     AEInstallEventHandler(kCoreEventClass,
  158.                           kAEQuitApplication,
  159.                           (EventHandlerProcPtr) MyHandleQUIT,
  160.                           0, false);
  161.  
  162.     AEInstallEventHandler(kCoreEventClass,
  163.                           kAEOpenDocuments,
  164.                           (EventHandlerProcPtr) MyHandleODOC,
  165.                           0, false);
  166.  
  167.     AEInstallEventHandler(kCoreEventClass,
  168.                           kAEPrintDocuments,
  169.                           (EventHandlerProcPtr) MyHandlePDOC,
  170.                           0, false);
  171. }
  172.  
  173.  
  174. /************************************************************
  175.   MyHandleOAPP - This is the "open application" AppleEvent
  176.   handler.  We check out the message, and set our "should we
  177.   quit after handling a 'pdoc'" flag to false.  If we got an
  178.   'oapp', we weren't launched via 'pdoc'.
  179.  
  180. *************************************************************/
  181.  
  182. pascal OSErr MyHandleOAPP(AppleEvent *theAppleEvent, AppleEvent *reply, long myRefCon)
  183. {
  184. #pragma unused (reply, myRefCon)
  185.     
  186.     gQuitAfterPrinting = false;
  187.     return MyCheckAEParams(theAppleEvent);
  188. }
  189.  
  190.  
  191. /************************************************************
  192.   MyHandleQUIT - This is the "quit" AppleEvent handler.  Set
  193.   the gQuitting flag and the event loop will do the rest.
  194.  
  195. *************************************************************/
  196.  
  197. pascal OSErr MyHandleQUIT(AppleEvent *theAppleEvent, AppleEvent *reply, long myRefCon)
  198. {
  199. #pragma unused (reply, myRefCon)
  200.  
  201.     gQuitting = true;
  202.     return MyCheckAEParams(theAppleEvent);
  203. }
  204.  
  205.  
  206. /************************************************************
  207.   MyHandleODOC - This is the "open document" AppleEvent
  208.   handler, so open some documents.
  209.  
  210. *************************************************************/
  211.  
  212. pascal OSErr MyHandleODOC(AppleEvent *theAppleEvent, AppleEvent *reply, long myRefCon)
  213. {
  214.     OSErr            err;
  215.     AEDescList        docList;
  216.     FSSpec            myFSS;
  217.     AEKeyword        theKeyword;
  218.     DescType        typeCode;
  219.     long            doc, itemsInList;
  220.     Size            actualSize;
  221.     MyDocumentPtr    newDocument;
  222.  
  223. #pragma unused (reply, myRefCon)
  224.  
  225.     gQuitAfterPrinting = false;
  226.  
  227. // Get the event's document list.
  228.  
  229.     err = AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &docList);
  230.     nrequire(err, AEError);
  231.  
  232.     err = MyCheckAEParams(theAppleEvent);
  233.     nrequire(err, CheckAEParamsFailed);
  234.     
  235.  
  236. // Open all entries in the document list.
  237.  
  238.     err = AECountItems(&docList, &itemsInList);
  239.     nrequire(err, AECountItemsFailed);
  240.  
  241.     for (doc = 1; (!err) && (doc <= itemsInList); doc++)
  242.     {
  243.         err = AEGetNthPtr(&docList, doc, typeFSS, &theKeyword, &typeCode,
  244.                           (Ptr) &myFSS, sizeof(FSSpec), &actualSize);
  245.     
  246.         nrequire(err, AEEntryError);
  247.  
  248.         err = MyCreateDocument(kDefaultTitle, &newDocument);
  249.         nrequire(err, CreateDocFailed);
  250.  
  251.         err = MyFSLoadDocument(newDocument, &myFSS, false);
  252.  
  253.         if (err == noErr)
  254.         {
  255.             ShowWindow(newDocument->documentWindow);
  256.             SelectWindow(newDocument->documentWindow);
  257.         }
  258.         else
  259.             MyDisposeDocument(newDocument);
  260.     }
  261.  
  262. CreateDocFailed:
  263. AEEntryError:
  264. AECountItemsFailed:
  265. CheckAEParamsFailed:
  266.     AEDisposeDesc(&docList);
  267.     MyAdjustMenus();
  268.  
  269. AEError:
  270.     return err;
  271. }
  272.  
  273.  
  274. /************************************************************
  275.   MyHandlePDOC - This is the "print document" AppleEvent
  276.   handler, so print some documents.
  277.  
  278. *************************************************************/
  279.  
  280. pascal OSErr MyHandlePDOC(AppleEvent *theAppleEvent, AppleEvent *reply, long myRefCon)
  281. {
  282.     OSErr            err;
  283.     AEDescList        docList;
  284.     FSSpec            myFSS;
  285.     long            itemsInList, i;
  286.     AEKeyword        theKeyword;
  287.     DescType        typeCode;
  288.     Size            actualSize;
  289.     MyDocumentPtr    newDocument;
  290.  
  291. #pragma unused (reply, myRefCon)
  292.  
  293. // Get our document list.
  294.  
  295.     err = AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &docList);
  296.     nrequire(err, AEError);
  297.  
  298. /*
  299.     Make sure we've accounted for all of the parameters
  300.     passed, and count the number of documents passed in.
  301. */
  302.  
  303.     err = MyCheckAEParams(theAppleEvent);
  304.     nrequire(err, AEError);
  305.     
  306.     err = AECountItems(&docList, &itemsInList);
  307.     nrequire(err, AEError);
  308.  
  309. /*
  310.     For each entry in the document list, load it,
  311.     print it, and close it.
  312. */
  313.  
  314.     for (i = 1; i<= itemsInList; i++)
  315.     {
  316.         err = AEGetNthPtr(&docList, i, typeFSS, &theKeyword, &typeCode,
  317.                           (Ptr) &myFSS, sizeof(FSSpec), &actualSize);
  318.  
  319.         nrequire(err, AEEntryError);
  320.  
  321. // Load the document.
  322.  
  323.         err = MyCreateDocument(kDefaultTitle, &newDocument);
  324.         nrequire(err, CreateDocFailed);
  325.  
  326.         err = MyFSLoadDocument(newDocument, &myFSS, true);
  327.         nrequire(err, LoadDocFailed);
  328.  
  329. //    Print the document.
  330.  
  331.         err = MyPrintDocument(newDocument);
  332.  
  333. // Close the document once it's printed.
  334.  
  335. LoadDocFailed:
  336.         MyDisposeDocument(newDocument);
  337.     }
  338.  
  339. // When we're all done throw away the description lists and return.
  340.  
  341. CreateDocFailed:
  342. AEEntryError:
  343.     AEDisposeDesc(&docList);
  344.  
  345. AEError:
  346.     if (gQuitAfterPrinting)
  347.         gQuitting = true;
  348.  
  349.     return err;
  350. }
  351.  
  352.  
  353. /************************************************************
  354.   MyCheckAEParams - This routine is used to make sure we've
  355.   received all the parameters for an AppleEvent.  If so, we
  356.   return noErr, otherwise we indicate that the event wasn't
  357.   handled completely.
  358.  
  359.  *************************************************************/
  360.  
  361. OSErr MyCheckAEParams(AppleEvent *theAppleEvent)
  362. {
  363.     OSErr        err;
  364.     DescType    typeCode;
  365.     Size        actualSize;
  366.     
  367.     err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr,
  368.                             typeWildCard, &typeCode, nil, 0, &actualSize);
  369.  
  370.     if (err == errAEDescNotFound) return noErr;
  371.     if (err == noErr) return errAEEventNotHandled;
  372.     
  373.     return err;
  374. }
  375.